package com.axc.gallery2.view;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;

import com.axc.gallery2.R;
import com.axc.gallery2.dbhelper.ImageInfo;

public class GridViewAdapter extends ImageAdapter {

	private List<Integer> state;
	private boolean canCheck = false;

	public GridViewAdapter(Context context, List<ImageInfo> lists) {
		super(context, lists);
		state = new ArrayList<Integer>();
	}

	// 设置是否可选择
	public void setCheckAble(boolean checked) {
		this.canCheck = checked;
		notifyDataSetChanged();
	}

	public boolean getCheckable() {
		return this.canCheck;
	}

	Bitmap bitmap = null;

	List<Integer> lstTimes = new ArrayList<Integer>();
	// 选择状态的view对象集合
	List<SoftReference<View>> convertviewCheckedList = new ArrayList<SoftReference<View>>();
	// List<View> convertviewCheckedList =new ArrayList<View>();
	// List<View> convertviewUnCheckedList =new ArrayList<View>();
	// 未选择状态的view对象集合
	List<SoftReference<View>> convertviewUnCheckedList = new ArrayList<SoftReference<View>>();
	long startTime = 0;
	private int w;

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		startTime = System.nanoTime();
		try {
			System.out.println("convertviewCheckedList"+convertviewCheckedList.size());
			System.out.println("convertviewUnCheckedList"+convertviewUnCheckedList.size());
			System.out.println("position"+position);
			if (canCheck) {
				if (convertviewCheckedList.size()-1 >= position) {
					convertView = convertviewCheckedList.get(position).get();
					if (convertView == null) {
						convertView = iniConvertView(convertView, position, 0);
					}

				} else {
					convertView = iniConvertView(convertView, position, 1);
				}
			} else {
				if (convertviewUnCheckedList.size()-1 >= position) {
					convertView = convertviewUnCheckedList.get(position).get();
					if (convertView == null) {
						convertView = iniConvertView(convertView, position, 0);
					}
				} else {
					convertView = iniConvertView(convertView, position, 1);
				}

			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return convertView;

	}

	/**
	 * 添加项目对象
	 * 
	 * @param convertView
	 * @param position
	 * @param oper
	 *            1为添加对象，其他为修改对象
	 * @return
	 */
	private View iniConvertView(View convertView, final int position, int oper) {
		final ViewHolder viewHolder = new ViewHolder();
		convertView = LayoutInflater.from(super.getContext()).inflate(
				R.layout.gridview_item, null);
		convertView.setTag(viewHolder);
		viewHolder.imageView = (ImageView) convertView
				.findViewById(R.id.listImageView);
		new AsyncLoadImage().execute(new Object[] { viewHolder.imageView,
				list.get(position).image_path,convertView
				.findViewById(R.id.progressBar1) });
		viewHolder.imageView.setImageBitmap(bitmap);
		viewHolder.checkBox = (CheckBox) convertView
				.findViewById(R.id.listCheckBox);
		if (canCheck) {
			viewHolder.checkBox.setVisibility(View.VISIBLE);
			viewHolder.checkBox.setChecked(getCheckValues(position));
			viewHolder.checkBox.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					boolean checkValue = viewHolder.checkBox.isChecked();
					setCheckItem(position, checkValue);
				}
			});
		} else {
			viewHolder.checkBox.setVisibility(View.INVISIBLE);

		}
		//旋转动作
//		convertView.setAnimation(AnimationUtils.loadAnimation(getContext(),
//				R.anim.gridview_rotate));
		convertView.setLayoutParams(new GridView.LayoutParams(w, w));
		convertView.setPadding(8, 8, 8, 8);

		int endTime = (int) (System.nanoTime() - startTime);
		lstTimes.add(endTime);
		if (true) {
			int total = 0;
			for (int i = 0; i < lstTimes.size(); i++)
				total = total + lstTimes.get(i);
			Log.e("10个所花的时间：" + total / 1000 + " μs", "所用内存："
					+ Runtime.getRuntime().totalMemory() / 1024 + " KB");
			lstTimes.clear();
			if (canCheck) {
				if (oper == 1) {
					convertviewCheckedList.add(new SoftReference<View>(
							convertView));
				} else {
					convertviewCheckedList.set(position,
							new SoftReference<View>(convertView));
				}
			} else {
				if (oper == 1) {
					convertviewUnCheckedList.add(new SoftReference<View>(
							convertView));
				} else {
					convertviewUnCheckedList.set(position,
							new SoftReference<View>(convertView));
				}
			}
		}
		return convertView;
	}

	// 设置选中项目
	public void setCheckItem(int position, Boolean isChecked) {
		System.out.println("position:" + position + "ischeck:" + isChecked);
		if(position==0){
			position=-1;
		}
		if (state.contains(position))
			state.remove((Object) position);

		if (isChecked) {
			state.add(position);
		}
		System.out.println("state.size" + state.size());
	}

	public boolean getCheckValues(int position) {
		if(position==0){
			position=-1;
		}
		return state.contains(position);
	}

	// 选择所有项目
	public void setCheckAll(boolean checkKey) {
		for (int i = 0; i < super.getList().size(); i++) {
			setCheckItem(i, checkKey);
		}
	}

	// 清除选择项目
	public void removeCheckItems() {
		for (Integer i : getCheckItemIds()) {
			if(convertviewCheckedList.size()>i)
			convertviewCheckedList.set(i, null);
			if(convertviewUnCheckedList.size()>i)
			convertviewUnCheckedList.set(i, null);
			convertviewCheckedList.remove(null);
			convertviewUnCheckedList.remove(null);

		}		
		for (ImageInfo path : getCheckItemPaths()) {
			list.remove(path);
		}
		getCheckItemIds().clear();
		getCheckItemPaths().clear();
	}



	// 获取选择的项目
	public List<Integer> getCheckItemIds() {
		return state;
	}

	/**
	 * 清除选中项目
	 */
	public void clearCheckItems() {
			state.clear();
	}

	// 获取选择的项目
	public List<ImageInfo> getCheckItemPaths() {

		int count = state.size();

		List<ImageInfo> ids = new ArrayList<ImageInfo>();

		for (int i = 0; i < count; i++) {

			ids.add(super.getList().get((int) state.get(i)));
		}

		return ids;

	}

	public void setGridviewSize(int w, int w2) {
		this.w = w;

	}

}
